package com.sfzlot.dev.netty.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xieqc on 2017/5/3.
 */
@Configuration
@EnableSwagger2
public class ApiSwaggerConfig {

    @Bean
    public Docket restDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("十方智联智能开关API文档")
                .apiInfo(apiInfo())
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.sfzlot.dev.netty.controller"))
                .paths(PathSelectors.any()).build().useDefaultResponseMessages(false).globalOperationParameters(addHeader());
    }

    /**
     * 添加头部参数
     * @return
     */
    private List<Parameter> addHeader(){
        //可以添加多个header或参数
        List<Parameter> list = new ArrayList<Parameter>();
        list.add(new ParameterBuilder().name("account").defaultValue("202008038888").description("账号").modelRef(new ModelRef("string")).parameterType("header").required(false).build());
        list.add(new ParameterBuilder().name("version").defaultValue("1.0.0").description("版本").modelRef(new ModelRef("string")).parameterType("header").required(false).build());
        list.add(new ParameterBuilder().name("timestamp").defaultValue("1615345801352").description("时间戳（毫秒）").modelRef(new ModelRef("string")).parameterType("header").required(false).build());
        list.add(new ParameterBuilder().name("signType").defaultValue("MD5").description("签名方式").modelRef(new ModelRef("string")).parameterType("header").required(false).build());
        list.add(new ParameterBuilder().name("sign").defaultValue("01b2bbaba0b1483a8825eb5b8683463c").description("签名值").modelRef(new ModelRef("string")).parameterType("header").required(false).build());
        return list;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("十方智联开发者平台")
                .description("智能开关相关数据接口")
                .termsOfServiceUrl("https://www.sfzlot.com").contact("技术开发部")
                .license("Licence Version 1.0").licenseUrl("#")
                .version("1.0.0")
                .build();
    }
}
